# Replication for "When group appeals backfire: explaining the asymmetric effects of place-based appeals"
# Authors: Lukas Haffert, Tabea Palmtag, Dominik Schraff

# Appendix
# This file replicates the analysis and figures included only in the appendix 
rm(list=ls(all=T))

knitr::opts_chunk$set(echo = TRUE, warnings=F, message=F, warning=F)
p_needed <- c("tidyverse","dplyr", "hrbrthemes","viridis", "rstatix", "stargazer", "sjPlot", "list", "margins", "table1")
lapply(p_needed, require, character.only = TRUE)

# Data survey I UK & GER ----
ger <- readRDS("germany_surveyI.Rds")
uk <- readRDS("uk_surveyI.Rds")

# Data survey II GER ----
df <- readRDS("germany_surveyII.Rds")

# Appendix A: Relationship between population density and urban/rural identification ----
## Figure A1 ----
ggplot(ger, aes(x = pop_density, y = as.factor(ruralurban))) +
  geom_point(position = position_jitter(width = 0.01), size = 1, color = "grey12") +
  labs(x = "Population density (ln)", y = "Self-assessed urbanity") +
  stat_summary(fun.y = "mean", geom = "crossbar", position = "identity", color="red") + theme_bw()
        
## Figure A2 ----
ggplot(uk, aes(x = pop_density, y = as.factor(ruralurban))) +
  geom_point(position = position_jitter(width = 0.1), size = 1, color = "grey12") +
  labs(x = "Population density (ln)", y = "Self-assessed urbanity") +
  stat_summary(fun.y = "mean", geom = "crossbar", position = "identity", color="red") + theme_bw()

## Figure A3 ----
ger <- ger %>%
  mutate(urbanruralchoice2 = 5- urbanruralchoice)
ggplot(ger %>% filter(!is.na(urbanruralchoice2)), aes(x = pop_density, y = as.factor(urbanruralchoice2))) +
  geom_point(position = position_jitter(width = 0.1), size = 1, color = "grey12") +
  labs(x = "Population density (ln)", y = "Number of urban fotos picked") +
  stat_summary(fun.y = "mean", geom = "crossbar", position = "identity", color="red") + theme_bw()

## Figure A4 ----
uk <- uk %>%
  mutate(urbanruralchoice2 = 5- urbanruralchoice)
ggplot(uk %>% filter(!is.na(urbanruralchoice2)), aes(x = pop_density, y = as.factor(urbanruralchoice2))) +
  geom_point(position = position_jitter(width = 0.1), size = 1, color = "grey12") +
  labs(x = "Population density (ln)", y = "Number of urban fotos picked") +
  stat_summary(fun.y = "mean", geom = "crossbar", position = "identity", color="red") + theme_bw()

## Figure A5 ----
ggplot(ger, aes(x = urbanruralchoice2, fill = factor(urbanrural))) +
  geom_histogram(position = "dodge", binwidth = 1) +
  scale_x_continuous(breaks = c(0:5))+
  scale_fill_viridis_d(name = "Urban/Rural Category", labels=c("Very urban","Urban","Rural" ,"Very Rural")) +  # Add legend title
  labs(x = "Number of urban fotos selected",
       y = "Share of respondents, DE",
       title = "Distribution of Urban Fotos by Urban/Rural Category in DE") +
  theme_bw() 

## Figure A6  ----
ggplot(uk, aes(x = urbanruralchoice2, fill = factor(urbanrural))) +
  geom_histogram(position = "dodge", binwidth = 1) +
  scale_x_continuous(breaks = c(0:5))+
  scale_fill_viridis_d(name = "Urban/Rural Category", labels=c("Very urban","Urban","Rural" ,"Very Rural")) +  # Add legend title
  labs(x = "Number of urban fotos selected",
       y = "Share of respondents, UK",
       title = "Distribution of Urban Fotos by Urban/Rural Category in UK") +
  theme_bw() 

# Appendix B: Measures of Place Identity and Resentment ----
uk_sub <- uk %>% mutate(`N Urban Foto` = urbanruralchoice, `Type of Place` = as.numeric(residence), `Urbanity` = as.numeric(urbanrural)) %>% select(`Type of Place`, `Urbanity`, `N Urban Foto`) 
ger_sub <- ger %>% mutate(`N Urban Foto` = urbanruralchoice, `Type of Place` = as.numeric(residence), `Urbanity` = as.numeric(urbanrural)) %>% select(`Type of Place`, `Urbanity`, `N Urban Foto`)

## Table B3 ----
cor(uk_sub, use = "complete.obs")
cor(ger_sub, use = "complete.obs")

# Appendix C: Descriptives Statistics, Study I ----
options(na.action = "na.pass") 
ger_mat_t <- model.matrix(~ gender + age + `Rural ID` + edu_cat - 1, 
                          data = subset(ger, ger$condition!="Control"))
ger_mat_t.df <- data.frame(ger_mat_t)  

ger_mat_c <- model.matrix(~ gender + age + `Rural ID` + edu_cat - 1, 
                          data = subset(ger, ger$condition=="Control"))
ger_mat_c.df <- data.frame(ger_mat_c)  

uk_mat_t <- model.matrix(~ gender + age + `Rural ID` + edu_cat - 1, 
                         data = subset(uk, uk$condition!="Control"))
uk_mat_t.df <- data.frame(uk_mat_t)  

uk_mat_c <- model.matrix(~ gender + age + `Rural ID` + edu_cat - 1, 
                         data = subset(uk, uk$condition=="Control"))
uk_mat_c.df <- data.frame(uk_mat_c)  

## Table C1 ----
stargazer(ger_mat_c.df, type = "text", title = "Descriptive statistics, control groups Germany")

## Table C2 ----
stargazer(ger_mat_t.df, type = "text", title = "Descriptive statistics, treatment groups Germany")

## Table C3 ----
stargazer(uk_mat_c.df, type = "text", title = "Descriptive statistics, control groups UK")

## Table C4 ----
stargazer(uk_mat_t.df, type = "text", title = "Descriptive statistics, treatment groups UK")

# Appendix D: Regression Results Survey 1 ----
## Table D2 ----
ger_urb <- ger %>% filter(urban=="urban") %>% filter(!is.na(eval_candidate))
m_ger1 <- lm(eval_candidate ~ treated*trust_parliament, data = ger_urb)
ger_rur <- ger %>% filter(urban=="rural") %>% filter(!is.na(eval_candidate))
m_ger2 <- lm(eval_candidate ~ treated*trust_parliament, data = ger_rur)
uk_urb <- ger %>% filter(urban=="urban") %>% filter(!is.na(eval_candidate))
m_uk1 <- lm(eval_candidate ~ treated*trust_parliament, data = uk_urb)
uk_rur <- ger %>% filter(urban=="rural") %>% filter(!is.na(eval_candidate))
m_uk2 <- lm(eval_candidate ~ treated*trust_parliament, data = uk_rur)

stargazer(m_ger1, m_ger2, m_uk1, m_uk2,
          dep.var.labels =	"Evaluation of Candidate depending on Trust in Parliament",
          column.labels = c("Germany Urban", "Germany Rural", "UK Urban", "UK Rural"),
          covariate.labels = c("Treated", "Trust in Parliament", "Treated x Trust"),
          table.placement = "ht!", column.sep.width = "0pt",
          label = "m_trust",
          digits = 2,
          title = "",
          type="text")

# Appendix E: Descriptive Statistics, Survey II ----
## Table E1 ----
options(na.action = "na.pass") 
c_df_mat <- model.matrix(~ gender + age + `Rural ID` + edu_cat - 1, data = subset(df, df$condition=="Control"))
c_df_mat.df <- data.frame(c_df_mat)  
stargazer(c_df_mat.df, type = "text", title = "Descriptive statistics survey 2, control group")

## Table E2 ----
t_df_mat <- model.matrix(~ gender + age + `Rural ID` + edu_cat - 1, data = subset(df, df$condition!="Control"))
t_df_mat.df <- data.frame(t_df_mat)  

stargazer(t_df_mat.df, type = "text", title = "Descriptive statistics survey 2, treatment groups")

## Table E3 ----
samplechar <- df %>%
  mutate(Age = ifelse(age < 28, "18-27", 
                      ifelse(age > 17 & age < 38, "28-37", 
                             ifelse(age > 37 & age < 48, "38-47", "48-57")))) %>%
  mutate(Gender = ifelse(gender == 1 , "male", "female")) %>%
  mutate(Employed = ifelse(empl_situation == "employed, full-time", "employed",
                           ifelse(empl_situation =="employed, part-time", "employed",
                                  ifelse(empl_situation == "self-employed", "employed", "not employed")))) %>%
  mutate(Income = ifelse(income ==1, "low income", 
                         ifelse(income ==2, "low income", 
                                ifelse(income ==3, "middle income", 
                                       ifelse(income ==4, "middle income", 
                                              ifelse(income ==5, "middle income", 
                                                     ifelse(income ==6, "middle income", "high income"))))))) %>%
  select(Age, Gender, Education, Employed, Income, `Residence Length`, Identity,`City Identity`, Resentment, Urbanity, Neighborhood) %>%
  na.omit()

caption  <- "Characteristics of urbanites with strong and weak identitiy"
table1(~ Age + Gender + Education + Employed + Income + `Residence Length` + `City Identity` + Resentment + Urbanity + Neighborhood | Identity, data=samplechar, caption=caption)

# Appendix F: Regression Results, Survey II ----
df <- df %>% filter(!is.na(eval_candidate))
m1 <- lm(eval_candidate ~ condition*Education, data = df)

new.data <- expand.grid(intercept=1, 
                        condition=unique(df$condition), 
                        Education=c("low", "middle", "high"))

coef_df <- data.frame(term=predict(m1, new.data, se=T)$fit,
                      se=predict(m1, new.data, se=T)$se.fit)%>%
  bind_cols(new.data) %>%
  mutate(Category = ifelse(condition=="Antagonistic/Innovation", "Antagonistic",
                           ifelse(condition=="Antagonistic/Rent", "Antagonistic",
                                  ifelse(condition == "Harmonious/Innovation", "Harmonious",
                                         ifelse(condition=="Rural", "Original", 
                                                ifelse(condition=="Urban", "Original",
                                                       ifelse(condition =="Harmonious/Rent", "Harmonious", 
                                                              ifelse(condition == "Control", "", NA)))))))) %>%
  mutate(Category = factor(Category,levels = c("","Original", "Antagonistic", "Harmonious"))) %>%
  mutate(conf.low=term-2*se,
         conf.high=term+2*se) %>%
  mutate(condition = ifelse(condition=="Antagonistic/Innovation", "Innovation",
                            ifelse(condition=="Antagonistic/Rent", "Rent",
                                   ifelse(condition == "Harmonious/Innovation", "Innovation",
                                          ifelse(condition =="Harmonious/Rent", "Rent", 
                                                 ifelse(condition=="Rural", "Rural", 
                                                        ifelse(condition=="Urban", "Urban",
                                                               ifelse(condition == "Control", "Control", condition))))))))

farben <- c("black","black")
position <- position_dodge2(width=.3)

## Figure F4 ----
ggplot(coef_df, aes(y = condition, x = term))+
  geom_point(aes(shape = Education), position=position, size=3) +
  geom_linerange(aes(xmin = conf.low, xmax = conf.high), position=position)+
  labs(title = "Candidate Evaluation")+
  scale_color_manual(values=farben)+
  labs(x="",y="")+
  theme_bw()+
  facet_grid(Category ~ ., scale = 'free_y', switch ="y")  +
  theme(panel.spacing = unit(0, "lines"), 
        strip.background = element_blank(),
        strip.placement = "outside", axis.ticks = element_blank(), panel.grid.minor = element_blank(), 
        legend.position="right",
        axis.text.y = element_text(size = 20), legend.text = element_text(size = 20), legend.title = element_text(size = 20), 
        axis.text.x = element_text(size = 20), plot.title = element_text(size = 25), strip.text.y = element_text(size = 15))


df <- df %>% filter(!is.na(candidate_competence))
m2 <- lm(candidate_competence ~ condition, data = df)

comp <- tidy(m2, conf.int = TRUE) %>%
  filter(term != "(Intercept)") %>%
  mutate(Category = ifelse(term=="conditionRural", "Original Appeals", 
                           ifelse(term=="conditionUrban", "Original Appeals",
                                  ifelse(term=="conditionAntagonistic/Innovation", "Antagonistic",
                                         ifelse(term=="conditionAntagonistic/Rent", "Antagonistic",
                                                ifelse(term == "conditionHarmonious/Innovation", "Harmonious",
                                                       ifelse(term =="conditionHarmonious/Rent", "Harmonious", NA))))))) %>%
  mutate(Category = fct_relevel(Category, "Original Appeals", "Antagonistic", "Harmonious")) %>%
  mutate(term =  ifelse(term=="conditionRural", "Rural", 
                        ifelse(term=="conditionUrban", "Urban",
                               ifelse(term=="conditionAntagonistic/Innovation", "Innovation",
                                      ifelse(term=="conditionAntagonistic/Rent", "Rent",
                                             ifelse(term == "conditionHarmonious/Innovation", "Innovation",
                                                    ifelse(term =="conditionHarmonious/Rent", "Rent", NA))))))) %>%
  mutate(Outcome="competence")

df <- df %>% filter(!is.na(candidate_credibility))
m3 <- lm(candidate_credibility ~ condition, data = df)

cred <- tidy(m3, conf.int = TRUE) %>%
  filter(term != "(Intercept)") %>%
  mutate(Category = ifelse(term=="conditionRural", "Original Appeals", 
                           ifelse(term=="conditionUrban", "Original Appeals",
                                  ifelse(term=="conditionAntagonistic/Innovation", "Antagonistic",
                                         ifelse(term=="conditionAntagonistic/Rent", "Antagonistic",
                                                ifelse(term == "conditionHarmonious/Innovation", "Harmonious",
                                                       ifelse(term =="conditionHarmonious/Rent", "Harmonious", NA))))))) %>%
  mutate(Category = fct_relevel(Category, "Original Appeals", "Antagonistic", "Harmonious")) %>%
  mutate(term =  ifelse(term=="conditionRural", "Rural", 
                        ifelse(term=="conditionUrban", "Urban",
                               ifelse(term=="conditionAntagonistic/Innovation", "Innovation",
                                      ifelse(term=="conditionAntagonistic/Rent", "Rent",
                                             ifelse(term == "conditionHarmonious/Innovation", "Innovation",
                                                    ifelse(term =="conditionHarmonious/Rent", "Rent", NA))))))) %>%
  mutate(Outcome="credibility")

df <- df %>% filter(!is.na(candidate_localunderstanding))
m4 <- lm(candidate_localunderstanding ~ condition, data = df)

local <- tidy(m4, conf.int = TRUE) %>%
  filter(term != "(Intercept)") %>%
  mutate(Category = ifelse(term=="conditionRural", "Original Appeals", 
                           ifelse(term=="conditionUrban", "Original Appeals",
                                  ifelse(term=="conditionAntagonistic/Innovation", "Antagonistic",
                                         ifelse(term=="conditionAntagonistic/Rent", "Antagonistic",
                                                ifelse(term == "conditionHarmonious/Innovation", "Harmonious",
                                                       ifelse(term =="conditionHarmonious/Rent", "Harmonious", NA))))))) %>%
  mutate(Category = fct_relevel(Category, "Original Appeals", "Antagonistic", "Harmonious")) %>%
  mutate(term =  ifelse(term=="conditionRural", "Rural", 
                        ifelse(term=="conditionUrban", "Urban",
                               ifelse(term=="conditionAntagonistic/Innovation", "Innovation",
                                      ifelse(term=="conditionAntagonistic/Rent", "Rent",
                                             ifelse(term == "conditionHarmonious/Innovation", "Innovation",
                                                    ifelse(term =="conditionHarmonious/Rent", "Rent", NA))))))) %>%
  mutate(Outcome="local understanding")

outcomes <- rbind(comp,cred,local)

## Figure F5 ----
ggplot(outcomes, aes(y = term, x = estimate))+
  geom_point(aes(shape = Outcome), position=position, size=3) +
  geom_linerange(aes(xmin = conf.low, xmax = conf.high), position=position)+
  labs(title = "Alternative Candidate Assessments")+
  scale_color_manual(values=farben)+
  labs(x="",y="")+
  theme_bw()+
  facet_grid(Category ~ ., scale = 'free_y', switch ="y")  +
  theme(panel.spacing = unit(0, "lines"), 
        strip.background = element_blank(),
        strip.placement = "outside", axis.ticks = element_blank(), panel.grid.minor = element_blank(), 
        legend.position="right",
        axis.text.y = element_text(size = 20), legend.text = element_text(size = 15), legend.title = element_text(size = 15), 
        axis.text.x = element_text(size = 20), plot.title = element_text(size = 25), strip.text.y = element_text(size = 15))

# Appendix G: Additional Results, Survey II ----
## Figure G4: Length of Residence in City ----
df1 <- df %>% filter(condition != "Urban", condition != "Rural") %>% filter(!is.na(eval_candidate))
m7 <- lm(eval_candidate ~ condition*`Residence Length`, data = df1)

stargazer(m7, 
          dep.var.labels =	"Evaluation of Candidate",
          covariate.labels = c("Antagonistic/Innovation", "Antagonistic/Rent", "Harmonious/Innovation", "Harmonious/Rent", "5-10 years in city", "> 10 years in city", "Always",  "Antagonistic/Innovation x 5-10 years", "Antagonistic/Rent x > 5-10 years", "Harmonious/Innovation x > 5-10 years", "Harmonious/Rent x > 5-10 years", "Antagonistic/Innovation x > 10 years", "Antagonistic/Rent x > 10 years", "Harmonious/Innovation x > 10 years", "Harmonious/Rent x > 10 years", "Antagonistic/Innovation x Always", "Antagonistic/Rent x Always", "Harmonious/Innovation x Always", "Harmonious/Rent x < Always"),
          table.placement = "ht!", column.sep.width = "0pt",
          label = "m_age",
          digits = 2,
          title = "",
          type="text")

## Figure G5: Municipal population density ----
m8 <- lm(eval_candidate ~ condition*Density, data = df)

stargazer(m8,
          dep.var.labels =	"Evaluation of Candidate (Survey 2)",
          covariate.labels = c("Antagonistic/Innovation", "Antagonistic/Rent", "Harmonious/Innovation", "Harmonious/Rent", "Municipal Population Density", "Antagonistic/Innovation x Population Density", "Antagonistic/Rent x Population Density", "Harmonious/Innovation x Population Density", "Harmonious/Rent x Population Density"),
          table.placement = "ht!", column.sep.width = "0pt",
          label = "m_context",
          digits = 2,
          title = "",
          type="text")  
